
package cz.muni.fi.pa165.carparkweb.actionbeans;

import cz.muni.fi.pa165.carpark.TO.BusinessTripTO;
import cz.muni.fi.pa165.carpark.TO.EmployeeTO;
import cz.muni.fi.pa165.carpark.TO.VehicleTO;
import cz.muni.fi.pa165.carpark.servicies.BusinessTripService;
import cz.muni.fi.pa165.carpark.servicies.EmployeeService;
import cz.muni.fi.pa165.carpark.servicies.VehicleService;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.DontValidate;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidateNestedProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author xmarton1
 */
@UrlBinding("/businesstrip/{$event}/{businesstrip.id}")
public class BusinessTripActionBean implements ActionBean {

    private ActionBeanContext context;
    private static final Logger log = LoggerFactory.getLogger(BusinessTripActionBean.class);
    @SpringBean
    protected BusinessTripService businessTripService;    
    @SpringBean
    protected EmployeeService employeeService;
    @SpringBean
    protected VehicleService vehicleService;
    
    @ValidateNestedProperties(value = { 
            @Validate(on = {"close2"}, field = "numberOfDrivenKms", required = true, minvalue=1), 
            @Validate(on = {"create"}, field = "vehicle.id", required = true),
            @Validate(on = {"con"}, field = "employee.id", required = true)   
    })    
    private BusinessTripTO businessTrip;
    
    @ValidateNestedProperties(value={
        @Validate(on={"create"}, field="id", required=true)
    })
    private EmployeeTO employee;
    
    private VehicleTO vehicle;
    
    private String vehicleFilter="-1";
    private String EmployeeFilter="-1";

    public String getVehicleFilter() {
        return vehicleFilter;
    }

    public void setVehicleFilter(String vehicleFilter) {
        this.vehicleFilter = vehicleFilter;
    }

    public String getEmployeeFilter() {
        return EmployeeFilter;
    }

    public void setEmployeeFilter(String EmployeeFilter) {
        this.EmployeeFilter = EmployeeFilter;
    }
     
    @Validate(required=true, on="getTripsByDates")
    private Date fromDate;
    @Validate(required=true, on="getTripsByDates")
    private Date toDate;

    public Date getFromDate() {
        return fromDate;
    }

    public void setFromDate(Date fromDate) {
        this.fromDate = fromDate;
    }

    public Date getToDate() {
        return toDate;
    }

    public void setToDate(Date toDate) {
        this.toDate = toDate;
    }
  
    
    @DefaultHandler
    @DontValidate()
    public Resolution all() {
        log.debug("businesstrip all()");
        return new ForwardResolution("/WEB-INF/jsp/businessTrip.jsp");
    }
    
    public Resolution add(){
        log.debug("businessTrip add()");
        return new ForwardResolution("/WEB-INF/jsp/businessTripCreate.jsp");
    }
    
    public Resolution create(){
        log.debug(" businessTrip create({})",businessTrip);
        EmployeeTO e = employeeService.retrieveEmp(employee.getId());
        VehicleTO v = vehicleService.retrieve(businessTrip.getVehicle().getId());
        businessTrip.setEmployee(e);
        businessTrip.setVehicle(v);
        businessTrip.setDateOfTrip(new Date());
        businessTripService.create(businessTrip);
        return new RedirectResolution(this.getClass(),"all");
    }
    
    public Resolution getTripsByDates(){
        log.debug("Trips from={} to={}",fromDate,toDate);
        return new ForwardResolution("/WEB-INF/jsp/businessTrip.jsp");
    }
    
    public Resolution getTripsByVehicle(){
        Long id = Long.parseLong(this.context.getRequest().getParameter("vehicleFilter"));
        if(id != -1){
            this.vehicle = this.vehicleService.retrieve(id);
        }
        else{
            this.vehicle = null;
        }
        return new ForwardResolution("/WEB-INF/jsp/businessTrip.jsp");
    }
    
    public Resolution getTripsByEmployee(){
        Long id = Long.parseLong(this.context.getRequest().getParameter("employeeFilter"));
        if(id != -1){
            this.employee = this.employeeService.retrieveEmp(id);
        }
        else{
            this.employee = null;
        }
        return new ForwardResolution("/WEB-INF/jsp/businessTrip.jsp");
    }
    
    public List<VehicleTO> getVehiclesForFilter(){
        return vehicleService.retrieveAllVehicles();
    }
    
    public List<EmployeeTO> getEmployeesForFilter(){
        return employeeService.retrieveAll();
    }
    
    public Resolution con(){
        log.debug("continue creating businessTrip");
        log.debug("cont({})",businessTrip);       
        EmployeeTO emp = businessTrip.getEmployee();        
        employee = employeeService.retrieveEmp(emp.getId());
        log.debug("cont({})",employee);   
        return new ForwardResolution("/WEB-INF/jsp/businessTripCreate2.jsp");
    }
    
    public Resolution delete(){
        log.debug("businessTrip delete({})",businessTrip);
        String id = context.getRequest().getParameter("businessTrip.id");
        this.businessTrip = businessTripService.retrieve(Long.parseLong(id));
        businessTripService.delete(businessTrip);
        return new RedirectResolution(this.getClass(),"all");
    }
    
    public Resolution back(){
        return new ForwardResolution("/WEB-INF/jsp/businessTripCreate.jsp");
    }
    
    public Resolution close(){
        String id = context.getRequest().getParameter("businessTrip.id");
        this.businessTrip = businessTripService.retrieve(Long.parseLong(id));
        return new ForwardResolution("/WEB-INF/jsp/businessTripClose.jsp");
    }
    
    public Resolution close2(){
        System.err.println(businessTrip);
        BusinessTripTO trip = businessTripService.retrieve(businessTrip.getId());
        businessTripService.cancelAbsolvedTrip(trip, businessTrip.getNumberOfDrivenKms()); 
        return new RedirectResolution(this.getClass(),"all");
    }
    

    public List<BusinessTripTO> getTrips(){
        
        List<BusinessTripTO> result;
        if(this.vehicle != null){
            result = businessTripService.retrieveVehiclesBusinessTrips(vehicle);
            this.vehicle = null;
            return result;
        }
        if(this.employee != null){
            result = businessTripService.retriveEmployeeBusinessTrips(employee);
            this.employee = null;
            return result;
        }
        result = businessTripService.retrieveAll();
        if(fromDate != null && toDate != null){
            result = businessTripService.retrieveByDates(result, fromDate, toDate);
            fromDate = toDate = null;
            return result;
        }
        return result;
    }
    
    public List<EmployeeTO> getEmployees(){
        return employeeService.retrieveAll();
    }
    
    public List<VehicleTO> getVehicles(){
        if(employee == null){            
            List<VehicleTO> list = new ArrayList<VehicleTO>();
            return list;
        }        
        return vehicleService.retrieveAllVehiclesForPosition(employee.getPosition());
    }
    
    @Override
    public void setContext(ActionBeanContext context) {
        this.context = context;
    }

    @Override
    public ActionBeanContext getContext() {
        return this.context;
    }

    public BusinessTripTO getBusinessTrip() {
        return businessTrip;
    }

    public void setBusinessTrip(BusinessTripTO businessTrip) {
        this.businessTrip = businessTrip;
    }

    public EmployeeTO getEmployee() {
        return employee;
    }

    public void setEmployee(EmployeeTO employee) {
        this.employee = employee;
    }

    public VehicleTO getVehicle() {
        return vehicle;
    }

    public void setVehicle(VehicleTO vehicle) {
        this.vehicle = vehicle;
    }
    
}
